package ClientCommTests;

import static org.junit.Assert.*;

import javax.swing.text.html.parser.TagElement;

import org.junit.Test;

import client.TelnetClient;
import clientCommunicationHandler.ClientServerMsgHandler;
import clientCommunicationHandler.UserInterfaceMsgHandler;

public class ClientServerMsgHandlerTest {
	private final String serverDelimiter = (char)1+"";
	@Test
	public void parseServerMsgLoginSuccessChangeUserNametest() {
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		TelnetClient.tempUserName = "tamir";
		tMsgHandler.parseServerMsg("login"+serverDelimiter+"success"+serverDelimiter);
		assertEquals("tamir", TelnetClient.userName);
	}

	@Test
	public void parseServerMsgLoginSuccessChangeMenu() {
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		TelnetClient.tempUserName = "tamir";
		tMsgHandler.parseServerMsg("login"+serverDelimiter+"success"+serverDelimiter);
		String menu = "[user: tamir] please make your choice:\n\t1. Logout\n\t2. view_Forums\n\t3. Exit";
		assertEquals(menu, TelnetClient.menu.toString());
	
	}
//	
//	@Test
//	public void getRestOfMenuTest(){
//		System.out.println("enter rest of menu");
//		ClientServerMsgHandler tClient = new ClientServerMsgHandler();
//		tClient.getRestOfTheMenu(2);//3 for user that is not log in
//		System.out.println(TelnetClient.menu);
//		assertEquals(TelnetClient.menu, "[user: tamir] please make your choice:\n\t1. Logout\n\t2. view_Forums\n\t3. Exit\n");
//	}
//	
	@Test 
	public void threadListaAdminMode(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter;
		TelnetClient.userName = "admin";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String realMenu = TelnetClient.menu.toString();
		String expectedMenu = "[user: admin] please make your choice:" +
				"\n\t1. Logout" +
				"\n\t2. viewThreads 1:temp1" +
				"\n\t3. add-moderator to subForum 1" +
				"\n\t4. remove-moderator of subForum 1" +
				"\n\t5. DeleteSubForum 1" +
				"\n\t6. add-new-SubForum" +
				"\n\t7. moderatorList";
		assertEquals(expectedMenu,realMenu);
		
	}
	
	@Test 
	public void threadListaModeratorMode(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String demoServerMsg = "threadsList"+serverDelimiter+"moderator"+serverDelimiter+"1:temp1"+serverDelimiter;
		TelnetClient.userName = "moderator";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String expectedMenu = "[user: moderator] please make your choice:\n\t1. Logout" +
				"\n\t2. viewThreads 1:temp1";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void threadListaownerMode(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String demoServerMsg = "threadsList\nowner\n1:temp1\n";
		TelnetClient.userName = "owner";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String expectedMenu = "[user: owner] please make your choice:\n\t1. Logout" +
				"\n\t2. viewThreads 1:temp1";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	
	@Test 
	public void addNewSubForum(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String demoServerMsg = "addedSubForum"+serverDelimiter+"success"+serverDelimiter+"admin"+serverDelimiter+"1"+serverDelimiter+"test"+serverDelimiter+"Tests"+serverDelimiter+"test"+serverDelimiter;
		TelnetClient.userName = "test";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String expectedMenu = "choose new moderator for sub forum 1:" +
				"\n\t1. test" +
				"\n\t2. Tests"+
				"\n\t3. test";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void addNewmoderatorToexistForum(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String demoServerMsg = "candidatesList"+serverDelimiter+"admin"+serverDelimiter+"test"+serverDelimiter+"Tests"+serverDelimiter+"test"+serverDelimiter;
		TelnetClient.userName = "test";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String expectedMenu = "[user: test] choose new moderator for sub forum 1:" +
				"\n\t1. Logout"+
				"\n\t2. test" +
				"\n\t3. Tests"+
				"\n\t4. test"+
				"\n\t5. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void getModeratorsList(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String demoServerMsg = "moderatorsList"+serverDelimiter+"admin"+serverDelimiter+"test"+serverDelimiter+"Tests"+serverDelimiter+"test"+serverDelimiter;
		TelnetClient.userName = "test";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String expectedMenu = "[user: test] moderators of subForum: 1:" +
				"\n\t1. Logout"+
				"\n\t2. remove test" +
				"\n\t3. remove Tests"+
				"\n\t4. remove test"+
				"\n\t5. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	
	public void testGetModeratorsList(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		TelnetClient.userName = "admin";
		ClientServerMsgHandler.subForumId  = "1";
		String demoServerMsg = "ModeratorsList\nUserRoll\nu1\nu2\n";
		tMsgHandler.parseServerMsg(demoServerMsg);
		String expected = "[user: admin] moderators of subForum: 1:\n\t1. Logout\n\t2. u1\n\t3. u2\n\t4. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expected, realMenu);
	}

	
	@Test
	public void parseServerMsgPostList(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String delimiter1 =(char)1+"";
		String delimiter2 =(char)2+"";
		String messageFromServer = "postList"+delimiter1+"member"+delimiter1+"1:p1"+delimiter2+"owner"+delimiter2+"this is body" +
				delimiter1+"2:p2"+delimiter2+"member"+delimiter2+"this is body2"+delimiter1+"\0\n";;
		TelnetClient.userName = "g";
		tMsgHandler.parseServerMsg(messageFromServer);
		String realMenu = TelnetClient.menu.toString();
		String expectedMenu = "[user: g] please make your choice:" +
				"\n\t1. Logout" +
				"\n\t2. post 1:p1 content: this is body" +
				"\n\t3. removePost 1:p1" +
				"\n\t4. editPost 1:p1" +
				"\n\t5. post 2:p2 content: this is body2" +
				"\n\t6. addPost 1" +
				"\n\t7. addPost 2" +
				"\n\t8. back\n";
		assertEquals(expectedMenu, realMenu);
//		assertEquals(newMenu,"\n\t1. threadList 3:temp1\n\t2. add\n\t3. back\n");
		
		
	}
	@Test
	public void parseServerMsgPosOwnerOfTwotList(){
		ClientServerMsgHandler tMsgHandler = new ClientServerMsgHandler();
		String delimiter1 =(char)1+"";
		String delimiter2 =(char)2+"";
		String messageFromServer = "postList"+delimiter1+"admin"+delimiter1+"1:p1"+delimiter2+"owner"+delimiter2+"this is body" +
				delimiter1+"2:p2"+delimiter2+"admin"+delimiter2+"this is body2"+delimiter1+"\0\n";;
		TelnetClient.userName = "g";
		tMsgHandler.parseServerMsg(messageFromServer);
		String realMenu = TelnetClient.menu.toString();
		String expectedMenu = "[user: g] please make your choice:" +
				"\n\t1. Logout" +
				"\n\t2. post 1:p1 content: this is body" +
				"\n\t3. removePost 1:p1" +
				"\n\t4. editPost 1:p1" +
				"\n\t5. post 2:p2 content: this is body2" +
				"\n\t6. removePost 2:p2" +
				"\n\t7. editPost 2:p2" +
				"\n\t8. addPost 1" +
				"\n\t9. addPost 2" +
				"\n\t10. back\n";
		assertEquals(expectedMenu, realMenu);
//		assertEquals(newMenu,"\n\t1. threadList 3:temp1\n\t2. add\n\t3. back\n");
		
		
	}
//	
//	

}
